package lib;

import com.memetix.mst.detect.Detect;
import com.memetix.mst.language.Language;

import lib.logging.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class DetectLanguage {

    public static String execute(File file) throws Exception {
        String text = readText(file);
        text = text.substring(0, 500);
        
        
        Detect.setKey("D6D3A40EDC77276322060DCC2004F50B6CCA351F");
        Language detectedLanguage = Detect.execute(text);
        return detectedLanguage.toString();
        
    }

    private static String readText(File file) {
        String text = "";
        FileInputStream is;
        try {
            is = new FileInputStream(file);
            int fileLength = (int) file.length();

            byte[] contents = new byte[fileLength];
            is.read(contents);

            text = new String(contents);

            is.close();
        } catch (FileNotFoundException e) {
            Logger.instance.error(Logger.stack2String(e));
        } catch (IOException e) {
            Logger.instance.error(Logger.stack2String(e));
        } finally {
        	
        }

        return text;
    }

}
